package main

import (
	"google.golang.org/grpc"
	"net"
	"fmt"
	"gowork/clientStream/message"
	"io"
	"time"
)

//订单服务实现
type OrderServiceImpl struct {
}

var OrderMap = map[string]*message.OrderInfo{
	"201907300001": &message.OrderInfo{OrderId: "201907300001", OrderName: "衣服", OrderStatus: "已付款"},
	"201907310001": &message.OrderInfo{OrderId: "201907310001", OrderName: "零食", OrderStatus: "已付款"},
	"201907310002": &message.OrderInfo{OrderId: "201907310002", OrderName: "食品", OrderStatus: "未付款"},
	"201907310003": &message.OrderInfo{OrderId: "201907310003", OrderName: "食品", OrderStatus: "未付款"},
	"201907310004": &message.OrderInfo{OrderId: "201907310004", OrderName: "食品", OrderStatus: "已付款"},
	"201907310005": &message.OrderInfo{OrderId: "201907310005", OrderName: "食品", OrderStatus: "未付款"},
	"201907310006": &message.OrderInfo{OrderId: "201907310006", OrderName: "食品", OrderStatus: "未付款"},
	"201907310007": &message.OrderInfo{OrderId: "201907310007", OrderName: "食品", OrderStatus: "未付款"},
	"201907310008": &message.OrderInfo{OrderId: "201907310008", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310009": &message.OrderInfo{OrderId: "201907310009", OrderName: "食品", OrderStatus: "未付款"},
	"201907310010": &message.OrderInfo{OrderId: "201907310010", OrderName: "食品", OrderStatus: "已付款"},
	"201907310011": &message.OrderInfo{OrderId: "201907310011", OrderName: "食品", OrderStatus: "未付款"},
	"201907310012": &message.OrderInfo{OrderId: "201907310012", OrderName: "食品", OrderStatus: "未付款"},
	"201907310013": &message.OrderInfo{OrderId: "201907310013", OrderName: "食品", OrderStatus: "未付款"},
	"201907310014": &message.OrderInfo{OrderId: "201907310014", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310015": &message.OrderInfo{OrderId: "201907310015", OrderName: "食品", OrderStatus: "未付款"},
	"201907310016": &message.OrderInfo{OrderId: "201907310016", OrderName: "食品", OrderStatus: "未付款"},
	"201907310017": &message.OrderInfo{OrderId: "201907310017", OrderName: "食品", OrderStatus: "未付款"},
	"201907310018": &message.OrderInfo{OrderId: "201907310018", OrderName: "食品", OrderStatus: "未付款"},
	"201907310019": &message.OrderInfo{OrderId: "201907310019", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310020": &message.OrderInfo{OrderId: "201907310020", OrderName: "食品", OrderStatus: "未付款"},
	"201907310021": &message.OrderInfo{OrderId: "201907310021", OrderName: "食品", OrderStatus: "未付款"},
	"201907310022": &message.OrderInfo{OrderId: "201907310022", OrderName: "食品", OrderStatus: "已付款"},
	"201907310023": &message.OrderInfo{OrderId: "201907310023", OrderName: "食品", OrderStatus: "未付款"},
	"201907310024": &message.OrderInfo{OrderId: "201907310024", OrderName: "食品", OrderStatus: "未付款"},
	"201907310025": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310026": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310027": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310028": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310029": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
	"201907310030": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
}

//添加订单信息服务实现
func (os *OrderServiceImpl) AddOrderList(stream message.OrderService_AddOrderListServer) error {
	fmt.Println(" 客户端流 RPC 模式")
	var orders message.OrderInfoList

	//orders := make([]*message.OrderInfo, 0)
	//orders = append(orders, &orderInfo)
	for {
		//从流中读取数据信息
		orderRequest, err := stream.Recv()
		if err == io.EOF {
			fmt.Println(" 读取数据结束 ")
			//result := message.OrderInfoList{OrderInfoList: orders}
			//return stream.SendAndClose(&result)
			return stream.SendAndClose(&orders)
		}
		if err != nil {
			fmt.Println(err.Error())
			return err
		}
		//打印接收到的数据
		fmt.Println(orderRequest.OrderList)

		go func() {
			for _, v := range orderRequest.OrderList {
				if (time.Now().Unix() >= v.TimeStamp) {
					orders.OrderInfoList = append(orders.OrderInfoList, OrderMap[v.OrderId])
				}
			}
		}()
	}
}

func main() {

	server := grpc.NewServer()
	//注册
	message.RegisterOrderServiceServer(server, new(OrderServiceImpl))

	lis, err := net.Listen("tcp", ":8091")
	if err != nil {
		panic(err.Error())
	}
	server.Serve(lis)
}
